jQuery(function($){

  //点击删除按钮，删除
  $("a.js-to-remove-my-product").on("click", function(){
    var _this = this
    BootstrapDialog.confirm('确定移除该商品吗？', function(result){
            if(result) {
              var id = $(_this).data("id");
              var csrf = $("input#js-sale-custom-product-index").val();
                $.ajax({
                  url: "/api/custom_products/"+ id +"/unuse",
                  method: 'put',
                  headers: {
                    'X-CSRF-Token': csrf
                  },
                  success: function(data){
                    if (data.status){
                      $(_this).parent().parent().remove();
                    }
                  }
                })
            }else {

            }
        });
  })

  //点击编辑按钮，使用js是防止在循环多次调用store_id。
  $("a.js-to-edit-my-custom").on("click", function(e){
    e.preventDefault();
    var id = $(this).data("id");
    var storeId = getUrlParam("store_id");
    location.href="/sales/custom_products/"+ id +"/edit?store_id="+storeId
  })


  // add my product to cart items
  $("a.js-add-my-custom-to-items").on("click", function(e){
    e.preventDefault();
    var _this = this
    BootstrapDialog.show({
            title: '提示：',
            message: "请输入数量: <input type='text' name='quantity' value='' placeholder='添加到购物车的数量' class='js-add-items-dialog-quantity'>",
            cssClass: 'add-to-items-dialog',
            buttons: [
              {
                label: '取消',
                cssClass: '',
                action: function(dialog){
                    dialog.close();
                }
              },
              {
                  label: '确定',
                  cssClass: 'btn-primary',
                  action: function(dialog){
                      var id = $(_this).data("id");
                      var storeId = getUrlParam("store_id");
                      var quantity = $("input.js-add-items-dialog-quantity").val();
                      var csrf = $("input#js-sale-custom-product-index").val();
            					$.ajax({
            						url: "/api/cart_items",
            						method: "post",
            						headers: {
                                  'X-CSRF-Token': csrf
                                },
            						data: {store_id: storeId, store_product_id: id, quantity: quantity},
            						success: function(data){
            							if(data.status == true){
                            showErrorView("已经成功加入购物车");
                            dialog.close();
            							}else {
                            dialog.close();
            								showErrorView(data.message);
            							}
            						}
            					})
                  }
              }
          ]
        });
  })
  // end add my product to items in index

})
